package com.android.activity.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.R;
import com.android.activity.adapter.UserRecordsAdapter;
import com.android.bean.BaseRecords;
import com.android.bean.GoodRecords;
import com.android.bean.GoodsType;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.bean.queryBean.UserGoodsRecords;
import com.android.model.goods.GoodsListener;
import com.android.model.goods.GoodsModelimpl;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessagePageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessagePageFragment extends Fragment implements GoodsListener {

    BaseRecords<UserGoodsRecords> recordsData;
    private View view;
    private RecyclerView recyclerView;
    private UserRecordsAdapter userRecordsAdapter;
    private User user;
    private GoodsModelimpl modelimpl;


    public MessagePageFragment() {
        // Required empty public constructor
    }


    public static MessagePageFragment newInstance(String param1, String param2) {
        MessagePageFragment fragment = new MessagePageFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

//    //获取用户交易记录
//    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
//    public void getUserGoodsRecords(BaseRecords<UserGoodsRecords> recordsData){
//        this.recordsData = recordsData;
//    }

    //获取当前用户信息
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getUserRes(ResponseData<User> responseData){
        User data = responseData.getData();
        if(data == null) {
            Log.e("unconnect", "can not get user data!!");
            return ;
        }
        this.user = data;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_message_page, container, false);

        modelimpl = new GoodsModelimpl();
        Log.e("11111",String.valueOf(user.getId()));
        Map<String, Object> map = new HashMap<>();
//        map.put("size",10);
        modelimpl.getUserRecords(user.getId(),map,this);

        return view;
    }

    private void initView() {
        recyclerView = view.findViewById(R.id.rv_message);
        if(recordsData.getRecords() != null){
            userRecordsAdapter = new UserRecordsAdapter(getActivity(),recordsData,user.getId());
            //准备布局管理器
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,
                    StaggeredGridLayoutManager.VERTICAL);
            //设置布局管理器的方向
            layoutManager.setReverseLayout(false);
            recyclerView.setLayoutManager(layoutManager);
            //设置布局管理器到RecyclerView中
            recyclerView.setAdapter(userRecordsAdapter);

        }
    }

    @Override
    public void onGetAllGoodsType(ResponseData<List<GoodsType>> responseData) {

    }

    @Override
    public void onGetAllGoodSuccess(ResponseData<GoodRecords> responseData) {

    }

    @Override
    public void onGetUserRecordsSuccess(ResponseData<BaseRecords<UserGoodsRecords>> recordsData) {
        BaseRecords<UserGoodsRecords> data = recordsData.getData();
        if(data == null) {
            Log.e("unconnect", "can not get user records data!!");
            return ;
        }
        this.recordsData = data;
        Log.e("MessagePageFragment",this.recordsData.getSize().toString());

        initView();
    }
}